package com.zx.tradeclient.ctrl.index6;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;

public class CustomerCenterActivity extends BaseActivity implements OnClickListener {
	/** Called when the activity is first created. */

	private TextView customer_center_phone;
	private Button customer_center_tel, customer_center_sub, customer_center_rewrite;

	private EditText customer_center_title, customer_center_content;

	private String phoneNumber = "";
	private TradeApp app;
	private ProgressDialog progressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customer_center);

		if (!(ActivityTaskManager.getInstance().containsName("CustomerCenterActivity"))) {
			ActivityTaskManager.getInstance().putActivity("CustomerCenterActivity", this);
		}

		init();

	}

	private void init() {

		app = (TradeApp) getApplication();

		setHeader(0);

		phoneNumber = getIntent().getStringExtra("phone");

		customer_center_phone = (TextView) findViewById(R.id.customer_center_phone);
		customer_center_phone.setText(phoneNumber);

		customer_center_tel = (Button) findViewById(R.id.customer_center_tel);
		customer_center_tel.setOnClickListener(this);

		customer_center_sub = (Button) findViewById(R.id.customer_center_sub);
		customer_center_sub.setOnClickListener(this);

		customer_center_rewrite = (Button) findViewById(R.id.customer_center_rewrite);
		customer_center_rewrite.setOnClickListener(this);

		customer_center_title = (EditText) findViewById(R.id.customer_center_title);
		customer_center_content = (EditText) findViewById(R.id.customer_center_content);

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {
			goback();
		} else if (v == customer_center_tel) {

			Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
			startActivity(intent);

		} else if (v == customer_center_sub) {
			if (customer_center_title.getText().toString().equals("")) {
				Toast toast2 = Toast.makeText(CustomerCenterActivity.this, "请输入标题", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();
			} else if (customer_center_content.getText().toString().equals("")) {
				Toast toast2 = Toast.makeText(CustomerCenterActivity.this, "请输入内容", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();
			} else {
				progressDialog = ProgressDialog.show(CustomerCenterActivity.this, null, "正在提交，请稍后.....", true, true);
				sendCustomentCenterInfo();
			}

		} else if (v == customer_center_rewrite) {
			customer_center_title.setText("");
			customer_center_content.setText("");
		}

	}

	private void sendCustomentCenterInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "leaveMessage");
		if (!(app.user.getId().equals(""))) {
			map.put("sendId", app.user.getId());
		}
		map.put("recvId", "FFFFFF");
		map.put("title", customer_center_title.getText().toString());
		map.put("content", customer_center_content.getText().toString());

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {

				try {
					String succeed = response.getString("succeed");

					if (succeed.equals("000")) {

						Toast toast2 = Toast.makeText(CustomerCenterActivity.this, "提交成功", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();

					} else {
						Toast toast2 = Toast.makeText(CustomerCenterActivity.this, "提交失败", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();

					}

					Intent intent = new Intent(CustomerCenterActivity.this, Index6Activity.class);
					startActivity(intent);

					progressDialog.dismiss();

				} catch (JSONException e) {
					progressDialog.dismiss();
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(CustomerCenterActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});

	}
}